<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>

    //阶乘
    function fn(num) {
      var sum1 = 1;
      for (var i = 1; i <= num; i++) {
        sum1 *= i;
      }
      return sum1;
    }
    console.log(fn(3))
    //写一个计算阶乘的函数 1！+2！+....X!
    function fn2(num) {
      var sum2 = 0;
      for (var i = 1; i <= num; i++) {
        sum2 += fn(i);
      }
      return sum2;
    }
    console.log(fn2(4))


    function fn3(num) {
      if (num == 1) {
        return fn(1);
      } else {
        return fn3(num - 1) + fn(num);
      }
    }
    console.log(fn3(5));
  </script>
</body>

</html>